(ns main.server
 (:use clojure.contrib.duck-streams utils.readbinfile utils.readDB utils.writeDB utils.misc)
 
 (:gen-class)

(:import (java.net Socket ServerSocket)
        (java.io PrintWriter)
        (java.io BufferedReader)
        (java.io InputStreamReader)))

(def *portS* 3003)

(defn auxiliar
	[_ objeto filename field-names field-sizes output]
	(let [	n			(read-string objeto)
			store    	(:new n)
			delete   	(:delete n)
			set-record	(:change n)] 
		(store-records store filename field-names field-sizes)
		(set-record-db set-record filename 74 field-sizes)
		(del-record-db delete filename 74 field-sizes)
	)
)

(defn accion-de-agente
	[_ socket field-names filename field-sizes]
	(with-open [input (BufferedReader. (InputStreamReader. (.getInputStream socket))) output (PrintWriter. (.getOutputStream socket))]
		(let [file (read-bin-file filename)
		          newinfo (serialize file)]
		      (.println output newinfo)
		      (.flush output))
		(let [update (.readLine input) puto (agent nil)]
			(clear-agent-errors puto)
			(send-off puto auxiliar update filename field-names field-sizes output)
	     )
	)
)

(defn sirve
	[]  
	(let [nombre-archivo (first *command-line-args*)
		servidor		(ServerSocket. *portS*)
		agente			(agent nil)
		filename		"db-1x2.db"
		rbf-output		(read-bin-file filename)		
		fields			(rbf-output :fields)
		field-names		(get-field-names fields)
		field-sizes		(get-field-sizes fields)]
(println "7777777777777777777777777777777777777777777777777777777777777777777777777777777
77777777777777777777777777777777II?=~~~~:~~~~:~~~=II777777777777777777777777777
77777777777777777777777777+~~~::,,,,,,,,,,,,,,,,,,,:=~~=I7777777777777777777777
7777777777777777777777+~~~:,,,,::===+===~~~===+==~~:,:,,:=~I7777777777777777777
777777777777777777?~~::,,:~==~:......,:..........,,,~=+=:,,:~=I7777777777777777
777777777777777?~~,:,:~+:,........:,,.,......,,.,:,.,:,,,~+:,,~~I77777777777777
7777777777777?:~,,,~=,,..:::,.,.,~,,..,,....:...,,..,:.,.,.:=:,,~=7777777777777
77777777777+~::,==,,:,~.,...,...,....,..,....,,.....,....,...,+~,,~+77777777777
777777777+~::,~~.,.,,,:.....,...,,,...,,,.,,.,,,..,,......,,.:,,=,:~=7777777777
77777777=~,,==,.,:~:.,,,...,.,......,,,,,,,,,,,:,,,,:,~=.,,,,,,,==~,~=777777777
7777777~,,:=,:..,~:,...,......,,.,:::::::::::::::::::::.:,..,..,,I=~,~=77777777
77777I~,,=:....,,.,.,,,,......,,~===~~~~~::~::::::::::~=??..,...=II=:::=7777777
7777I~,,+.......,,..=:.,,,,,.,,~+++==~~~~~:~~::::::::::~=?~.,,,.II?I=:,=I777777
7777~,,=.,...::..,:,.,......,:~=++++==~~~~~~~~::::::,:::=+I....~??II?=,,~777777
777=,,=:,...,,,,,,,,,...,,.,,:~=+++++==~~~~~~~~~::::,:::~=+=..,I????I+~,~=77777
77I~,~:.......,,....,,,,,.,,:~~=+++=::::::~=~~::::::::,,,:~I.~I??????I=:,=I7777
77+,:=,..,.::,,.,......,,.,:~~~~=:,:~::,....:~~::::::,,,,,:=~II??????I+=,:=7777
77~,:~,.,,...,:,...~~:,..,::~~~~:~==~:::::::::::::::,,:=~,.:??????????I=,,~I777
7+:,+,..,.,~:..,...,,,,,.:::~~::~~~::,..,:,,:::::::,,:~~::.,~??????????+::~+777
7=:,+,,,..::,,.,....,,,,,::~~~::::.,,...,::,:~=~:::,,::::,,:~???????????~,:=777
7=,,=,,,.,,,..,.,::,.,,.,:~~~===~:,?,:.,I7=~=~~=~,,.,=:+.:+.=???????????=,,=777
7=,,=,.,,,,,:,,....,,,,,,:~~=====~~~:::::::~=~=+=:~,,~~~=~~++???????????=:,=777
7=,:~,....,,,,..,::,,,.,,::~===+===~~:~:~=+=:~~=+~,=.,:,:::::+????+?????=~,~777
7~,,~....,::,,,.:..,.,,,,:~~=++++==~~~::~~:::~=~==~:?.,.:~~~=+?++++++++++~,:I77
7=,:=.,,,,:,..,,,..,,,,,::~~~=++?+++=~~~~::,===+?=~:=?:.,,,:=+++++++++++?=,:I77
7=,,+...,,,,,...,,:,,,,,:::~~~==+??+=~::::,:=:,,~~::,,:.~=::::+?+++++++++=,:I77
7?:,+.,....,,...,:,,.,:,:~~~~~=====~~::::,=~:~=~::,.,,,:+==~::=++++++++++=,:777
77~,~:...,.,...,,,,,.~7I,~~~~~~~~~~::::,,=++~~~~~==::,,,++=~::=++++++++++=,:777
77=,:==,.,..,.,,,,,.,?++.::~~:::::::::,,~~==~===::~~:,,,~=~~::+++++++++++=,:777
77+:,=++::,,....,,,:..,.:::~~~::::::,,.~===~:::::::::::,,~~~,~?++++++++++~,:777
777=,:=?+:....,,::,.,...:~~~===~~::,,,~~:~?I777777777I=:,=~:~+++++++++++=:,=777
777=,,=+???~:,...,.,,,,.,:~~===~~~:,,,,~~=+==~~~~:::::,,:+=:++++++++++++=,~?777
7777~,:=?+++?==,.,,,,...,:~~===~~~~:,:=~:::::::::::,,,:::+~~++++++++++++:,~7777
7777~,,=++++++++:....,,..:::~==~~~~:~=++==~~=~~~~~~~~::::=~=+++++++++++=,~?7777
7777I~,:++++++++?+~~.,.,.,,,:~~~~~~~~+++=++++=~~~~~::,~~~~~+++++++++++=,:=77777
77777+~,=+++++++++++++=:.,,,::~=~~=======~~~~:::,,,,,:~::~+++++++++=+=::~777777
777777~,:=+++++++++++++++~,,:::~~~~==+==~~~~~::::,,,,::::=+=+++++++==:,~7777777
777777I~,,=++++++++++++=+7,,,:::~~:::~~~=====~~====~::::=+++++++++==,,~77777777
77777777~,,=+++++++++++=I7I,::::::~:::::~===+?I+==~~:::=+++++++++=~::=777777777
777777777+~,:=+++++++===+7777:::::::::::,::~~~~==~~~~:~=+++=++++==,,~=777777777
777777777 I~,,~==+=++++=I777777~:::::::::,,,:::::::~=+++=++++==~,,~=77777777777
77777777777+~,,~=+++==?77777I777=~~~::::::,,,,,,,:+=++=++===,,,~=77777777777777
777777777777I=:,,===+=I7I777777777+~::::,:,,,,,,,~++++===:,,,==7777777777777777
77777777777777+:,,,~==?777777777777I=~~:,,,,,,,:=+==~:,,:~=77777777777777777777
7777777777777777+:,,,,~~~==+IIII77777I=~~===+=::,,,:~=?777777777777777777777777
777777777777777777777+~,,,,,,,::~~~~~~~~~::,,,,,,:~==I7777777777777777777777777
77777777777777777777777777I++=~::::::::::~~~~=+I7777777777777777777777777777777
7777777777777777777777777777777777777777777777777777777777777777777777777777777")
	(loop []
		(println "\n Serving Hotcakes and Petitons")      
		(let [socket (.accept servidor)]
			(clear-agent-errors agente)
			(send-off agente accion-de-agente socket field-names filename field-sizes) 
        (recur)))))

